***************************************************************************************************************
* Name: FigureA4_dataset
* Description: saves dataset used as input for Figure A4
* Reference: "Competition and Civilian Victimization"
***************************************************************************************************************

use "violence_t_year_export.dta",clear
keep if victim_farc~=.&victim_paras~=.&victim_eln~=.&nbi_t~=.&royalties_t~=.&coca_t~=.&share_left~=.&lpobl_tot_t~=.&army_dist~=.&dmr~=.&gini_i~=.&evlp~=.&gcaribe~=.&gpacifica~=.&gorinoquia~=.&gamazonia~=.&farc_dist~=.&paras_dist~=.&eln_dist~=.&year~=.

*****Log likelihood definitions*****

*Victimization restricted
global vF "victim_farc"
global vP "victim_paras"
global vE "victim_eln"
 

*Victimization un-restricted
capture program drop lfsec_ur
program lfsec_ur
  version 15.1
  args lnf xbF xbP xbE
  tempvar pF pP pE
  quietly {
     gen double `pF' = 1/(1+exp(-`xbF'))
     gen double `pP' = 1/(1+exp(-`xbP'))
	 gen double `pE' = 1/(1+exp(-`xbE'))
     replace `lnf' = $vP*ln(`pP') + (1-$vP)*ln(1-`pP') + $vF*ln(`pF') + (1-$vF)*ln(1-`pF')+ $vE*ln(`pE') + (1-$vE)*ln(1-`pE') 
   }
end



 capture program drop game2stage
 program game2stage, eclass
	syntax [if]
	marksample touse
	tempname b dF_e dP_e dF_p dP_f dE_p dE_f 
	tempvar predF predP predE
	local controls "nbi_t royalties_t coca_t share_left lpobl_tot_t i.year army_dist dmr gini_i evlp gcaribe gpacifica gorinoquia gamazonia"
	*local interactions "	"
	
	quietly {
		npregress kernel victim_farc `controls' farc_dist if victim_paras~=.,estimator(constant) noderivatives
		predict `predF'
		npregress kernel victim_paras `controls' paras_dist if victim_paras~=.,estimator(constant) noderivatives
		predict `predP'
		npregress kernel victim_eln `controls' eln_dist if victim_paras~=.,estimator(constant)  noderivatives
		predict `predE'
	}
	
	gen pF=`predF'
	gen pP=`predP'
	gen pE=`predE'
		
	ml model lf lfsec_ur (eq1: victim_farc = nbi_t royalties_t coca_t share_left lpobl_tot_t  army_dist ///
						 dmr year_d1-year_d7 gini_i  evlp farc_dist pP pE gcaribe gpacifica gorinoquia gamazonia) (eq2: nbi_t royalties_t coca_t ///
						share_left lpobl_tot_t army_dist dmr gini_i year_d1-year_d7  ///
						 evlp paras_dist pF pE gcaribe gpacifica gorinoquia gamazonia) (eq3:nbi_t royalties_t coca_t /// 
						share_left lpobl_tot_t army_dist dmr gini_i year_d1-year_d7 evlp eln_dist pF pP gcaribe gpacifica gorinoquia gamazonia)
	capture noisily ml maximize, diff 
	display "maximize" _rc
	if _rc~=0 {
		matrix `b'=J(1,72,0)
		matrix `dF_e'=J(1,13,0)
		matrix `dF_p'=J(1,13,0)
		matrix `dP_e'=J(1,13,0)
		matrix `dP_f'=J(1,13,0)
		matrix `dE_p'=J(1,13,0)
		matrix `dE_f'=J(1,13,0)		
	}
	else{
		
		matrix `b'=e(b)

		quietly{
			margins , at(pP=(0.2(0.05)0.8) pE=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1))))) nose
			matrix `dF_p'=r(b)
			
			margins , at(pE=(0.2(0.05)0.8) pP=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1))))) nose
			matrix `dF_e'=r(b)

			margins , at(pF=(0.2(0.05)0.8) pE=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq2))))) nose
			matrix `dP_f'=r(b)
			
			margins , at(pE=(0.2(0.05)0.8) pF=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq2))))) nose
			matrix `dP_e'=r(b)
			
			margins , at(pF=(0.2(0.05)0.8) pP=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3))))) nose
			matrix `dE_f'=r(b)
			
			margins , at(pP=(0.2(0.05)0.8) pF=0.2 year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0) atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3))))) nose
			matrix `dE_p'=r(b)
			}
	}		
	matrix `b'=(`b',`dF_p',`dF_e',`dP_f',`dP_e',`dE_f',`dE_p')		
		
	drop pF pP pE
	ereturn post `b' ,esample(`touse')		
	
end


game2stage
matrix b=e(b)
matrix res_boot=b
set seed 1313

set matsize 800

forvalues i = 1/500{
	preserve
	bsample
	game2stage
	matrix b=e(b)
	matrix res_boot=(res_boot\b)
	display `i'
	restore
}

svmat double res_boot
keep res_boot1-res_boot150
save "boot_res_eln_year.dta",replace
